<?php
/**
 * Created by PhpStorm.
 * User: 96151
 * Date: 2018/11/7
 * Time: 20:15
 */
namespace app\common\service\swooleJobs;
use app\common\model\article\PublishArticleModel;
use app\common\model\spider\SpiderArticleModel;
use app\common\model\spider\SpiderDetailModel;
use app\common\redis\ArticleParticipleRedis;
use app\common\service\ElkLog;
use app\common\service\PythonApiService;
use app\common\service\SwooleJob;

class SpiderArticleJobs
{
    // 类型
    const CONTENT_TYPE_ARTICLE = 1;

    /**
     * 加入推送队列
     */
    public static function spiderArticleTagPush( $id, $type = self::CONTENT_TYPE_ARTICLE )
    {
        SwooleJob::push( 'MyJob',__CLASS__ ,'spiderArticleTag', array( $id, $type ) );
    }

    /**
     * 采集文章异步打标签及关联币种
     * @param $id
     * @param int $type
     * @return bool
     */
    public function spiderArticleTag( $id, $type = self::CONTENT_TYPE_ARTICLE )
    {
        ElkLog::write( array( 'message'=> 'spiderArticleTag', 'data'=> array( 'id'=> $id, 'type' => $type ) ) );
        $articleModel = new SpiderArticleModel();
        $SpiderDetailModel = new SpiderDetailModel();
        $py_api = new PythonApiService();
        $PublishArticleModel = PublishArticleModel::getInstance();

        $data_detail = $SpiderDetailModel->where( array('id' =>$id))->find();
        $data = $articleModel->where( array('id' => $id))->find();

        if ( !$data || !$data_detail ){
            return false;
        }
        
        $data_detail['detail'] = strip_tags( $data_detail['detail'] );

        if ( $data['article_type'] == $articleModel::CMS_TYPE_NEWS_FLASH ){
            $data_detail['detail'] = preg_replace( "/【.*?】/",'',$data_detail['detail'] );
        }

        $similarity_data = $PublishArticleModel->getSimilarity( $data['title'].$data_detail['detail'], $id, ArticleParticipleRedis::ARTICLE_TYPE_SPIDER );

        $update = array(
            'similarity' => $similarity_data['similarity'],
            'similarity_type' => $similarity_data['article_type'],
            'similarity_id' => $similarity_data['similarity_article_id']
        );

        // 新版快讯修改    查重降至86%
        $ratio = $data['article_type'] == $articleModel::CMS_TYPE_NEWS_FLASH  ? 86 : 95;

        if ( $similarity_data['similarity'] >$ratio ){
            $update['delete'] = 1;
            $articleModel->update_data( $id, $update );
            return false;
        } else {
            $articleModel->update_data( $id, $update );
        }

        // 2018-12-24 江通新需求
//        $res = $py_api->get_content_tags( $data['title'].$data_detail['detail'] );
        $res = $py_api->get_content_tags( $data['title'] );

        $coin_list = [];

        // 获取币种
        foreach ( $res as $value ) {
            $coin_list = array_merge( $coin_list, $value['token_id'] );
        }

        $coin_list = array_unique( $coin_list );

        $coin_list = array_map(function($value){
            return (int)$value;
        },$coin_list);

        $coin_list = array_values( $coin_list );

        $update_data = array(
            'coin_list' => json_encode($coin_list)
        );

        // 文章分词
        $PublishArticleModel->articleParticiple( $id, $data['title'].$data_detail['detail'] , ArticleParticipleRedis::ARTICLE_TYPE_SPIDER );

        // 将刚处理完的hash加入分词
//        if ( PublishArticleModel::$ArticleHashList ){
//            PublishArticleModel::$ArticleHashList[ArticleParticipleRedis::ARTICLE_TYPE_SPIDER.':'.$id] = $hash;
//        }

        $articleModel->update_data( $id,$update_data);
        return true;
    }

}